package com.boxin.ims.modules.wechat.busi;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.bi.dao.BIDao;
import com.boxin.ims.modules.bi.service.AliveMemberService;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatMemberService;

/**
 * @author Jakemanse
 * @date 2013-12-7
 * @function <p>获得管理员数据 </p>
 */
@Service
public class BiMemberMessageByAdminProcessor extends BusinessProcessor{
	
	
	@Autowired 
	private BIDao biDao;
	
	@Autowired
	private AliveMemberService aliveMemberService;
	
	@Autowired
	private WechatMemberService wechatMemberService;

	@Override
	public ProcessMessage process() {
		
		WechatMember  member = memberCheckAndReply();
		if("1".equals(member.getIsAdmin())){
			
			
			//签到人数
			
			Map<String, Object> params=new HashMap<String, Object>();
			
			params.put("day", DateFormatUtils.format(new Date(), "yyyyMMdd"));
			
			params.put("user_id", user.getId());
			params.put("message", "签到");
			
			List<Map<String, Object>>  signupList=    biDao.queryMessageByDay(params);
//			
			
			//关注数
			params.clear();
			params.put("day", DateFormatUtils.format(new Date(), "yyyyMMdd"));
			params.put("user_id", user.getId());
			params.put("event_type", "subscribe");
			
			List<Map<String, Object>>  subscribeList=    biDao.queryMessageByDay(params);
			
			
			//会员总数
			
			Long count =  wechatMemberService.getMemberCount(user.getId());
			
			//注册会员数
			params.clear();
			params.put("user_id", user.getId());
			params.put("day", DateFormatUtils.format(new Date(), "yyyyMMdd"));
			
			List<Map<String, Object>>  memberList=    biDao.queryBiWechatMemberByDay(params);
			
			//当日退订数
			
			params.clear();
			params.put("user_id", user.getId());
			params.put("event_type", "unsubscribe");
			params.put("day", DateFormatUtils.format(new Date(), "yyyyMMdd"));
			
			List<Map<String, Object>>  unSubscribeList=    biDao.queryMessageByDay(params);
			
			
			//当日活跃人数
			Long memberCount =  aliveMemberService.getAliveMemberCountByDay( user.getId(), DateFormatUtils.format(new Date(), "yyyyMMdd"));
			
			
			String reply=DateFormatUtils.format(new Date(), "yyyy-MM-dd")+"数据报告\n";
			
			if(signupList.size()==0){
				reply+="签到数：0人";
			}else{
				reply+="签到数："+signupList.get(0).get("count").toString();
				
			}
			
			if(subscribeList.size()==0){
				reply+="\n关注数：0人";
			}else{
				reply+="\n关注数："+subscribeList.get(0).get("count").toString();
				
			}
			
			if(memberList.size()==0){
				reply+="\n注册会员数：0人";
			}else{
				reply+="\n注册会员数："+memberList.get(0).get("count").toString();
				
			}
			
			if(count==null){
				reply+="\n会员总数：0人";
			}else{
				reply+="\n会员总数："+count;
				
			}
			
			if(unSubscribeList.size()==0){
				reply+="\n当日退订数：0人";
			}else{
				reply+="\n当日退订数："+unSubscribeList.get(0).get("count").toString();
			}
			
			
			reply+="\n当日活跃人数："+memberCount;
			
			reply+="\n<a href=\"http://weixin.zhaotang.net/zt/f/site/biData?mid="+member.getId()+"&uid="+user.getId()+"\" >点击查看详细</a>";
			
			writeToUser(reply);
			
			
		}else{
			writeToUser("对不起！您还没有权限，请联系管理员！！");
		}
		
		
		
		return null;
	}

	
	@Override
	public boolean isNeedBeMember() {
		return false;
	}

}
